package cn.j229.bug.zhongxuan.hualian.utils;

/**
 * @Author smile_ZhongXuan
 * @Date 2020/10/21 11:18
 * @Version 1.0
 */

import java.util.Random;
import java.util.UUID;

public class RandomCheckCode {

    //生成随机数字和字母,
    public static String getRandomCheckCodeLetterAndNumber() {

        String value = "";
        Random random = new Random();

        //参数length，表示生成几位随机数
        int length = 6;
        for(int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                value += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                value += String.valueOf(random.nextInt(10));
            }
        }
        return value;
    }

    public static String getRandomCheckCodeNumberMethodOne(){
        // 生成6位随机数验证码(0-9)
        int length = 6;
        String code = "";
        for(int i = 0; i < length; i++){
            code += (int)(Math.random()*9);
        }
        return code;
    }

    public static String getRandomCheckCodeNumberMethodTwo(){
        Random random = new Random();
        // 生成6位随机数验证码(0-9)
        int length = 6;
        int start = random.nextInt(11-length);
        int end = start + length;
        int checkCode = UUID.randomUUID().toString().hashCode();
        if(checkCode < 0) {
            checkCode = - checkCode;
        }
        String format = String.format("%010d", checkCode);
        String code = format.substring(start, end);
        return code;
    }
}
